import java.util.LinkedList;

public class L222 {
    class Solution {
        public int countNodes(TreeNode root) {
            if (root == null) return 0;
            LinkedList<TreeNode> queue = new LinkedList<>();
            int count = 0;
            queue.offer(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                count += size;
                while (size-- > 0) {
                    TreeNode poll = queue.poll();
                    if (poll.left != null) queue.offer(poll.left);
                    if (poll.right != null) queue.offer(poll.right);

                }
            }
            return count;
        }
    }
}
